<?php
class M_setup extends CI_Model{

	function get_param($param = null){
		$sql = "
		SELECT param1, nvalue1 FROM m_setup";
		
		if($param != null)
			$sql .= " WHERE lower(param1)  = '".mysql_real_escape_string(strtolower($param))."'";

		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	function update($data_where,$data_submit){
		$this->db->where($data_where);
		$this->db->update("m_setup",$data_submit);
	}
	
	function save($id,$data,$tipe){
		if ($tipe == "pic"){
			if ($id == ""){
				$this->db->insert('m_pic',$data);
			}else{
				$this->db->where('pic_id',$id);
				$this->db->update('m_pic',$data);
			}
			echo 1;
		}else{
			if ($id == ""){
				$this->db->insert('m_owner',$data);
			}else{
				$this->db->where('owner_id',$id);
				$this->db->update('m_owner',$data);
			}
			echo 1;
		}
	}
	
	function ubah_password($data){
		$this->db->where('user_id',$_SESSION[SESSION_USERID]);
		$this->db->update('s_users',$data);
		echo 1;
	}
	
	function list_pic($option,$id = 0,$page = 1 ,$rows = 10, $pic_name = ''){
		if ($option == "single"){
			$sql = "
			SELECT * FROM m_pic 
			where pic_id = '".mysql_real_escape_string($id)."'
			ORDER BY pic_id DESC ";
			
			$query = $this->db->query($sql);
			$result = $query->result();
			return json_encode($result);
		}else{
			$sql = "
			SELECT pic_id, pic_name ,pic_active,
			concat('<span class=\"btn-group\"><input type=button class=\"btn btn-info\" value=\"Edit\" onClick=\"edit(',pic_id,',\'pic\')\" /><input type=button class=\"btn btn-error\" value=\"Delete\" onClick=\"hapus(',pic_id,',\'pic\')\" /> </span>') as action
			FROM m_pic ";
			if ($pic_name != "")
				$sql .= " WHERE pic_name like '%".$pic_name."%'";
			$sql .= "ORDER BY pic_id DESC ";
			
			$query = $this->db->query($sql);
			$records = $query->num_rows();
			
			$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
			$query = $this->db->query($sql);
			$result = $query->result();
			return '{ 
			  "total": "'.ceil($records / $rows).'", 
			  "page": "'.$page.'", 
			  "records": "'.$records .'",
			  "rows" : '.json_encode($result).'
			}';
		
		}
	}
	
	function list_owner($option,$id = 0,$page = 1 ,$rows = 10, $owner_nama = ''){
		if ($option == "single"){
			$sql = "
			SELECT * FROM m_owner 
			where owner_id = '".mysql_real_escape_string($id)."'
			ORDER BY owner_id DESC ";
			
			$query = $this->db->query($sql);
			$result = $query->result();
			return json_encode($result);
		}else{
			$sql = "
			SELECT owner_id, owner_nama, owner_jenis ,owner_aktif,
			concat('<span class=\"btn-group\"><input type=button class=\"btn btn-info\" value=\"Edit\" onClick=\"edit(',owner_id,',\'owner\')\" /><input type=button class=\"btn btn-error\" value=\"Delete\" onClick=\"hapus(',owner_id,',\'owner\')\" /> </span>') as action
			FROM m_owner ";
			if ($owner_nama != "")
				$sql .= " WHERE owner_nama like '%".$owner_nama."%'";
			$sql .= "
			ORDER BY owner_id DESC ";
			
			$query = $this->db->query($sql);
			$records = $query->num_rows();
			
			$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
			$query = $this->db->query($sql);
			$result = $query->result();
			return '{ 
			  "total": "'.ceil($records / $rows).'", 
			  "page": "'.$page.'", 
			  "records": "'.$records .'",
			  "rows" : '.json_encode($result).'
			}';
		
		}
	}
	
	function delete($data,$tipe){
		if ($tipe == "pic"){
			$this->db->delete('m_pic',$data);
			echo 1;
		}else{
			$this->db->delete('m_owner',$data);
			echo 1;
		}
	}
}